﻿#pragma once

enum Colour
{
	RED,
    BLACK
};
template <class K,class V>
struct RBTreeeNode
{
	pair<K, V> _kv;
	RBTreeeNode<K, V>* _left;
	RBTreeeNode<K, V>* _right;
	RBTreeeNode<K, V>* _parent;
	Colour _col;

	RBTreeeNode(const pair<K, V>& kv)
		:_kv(kv)
		,_left(nullptr)
		,_right(nullptr)
		,_parent(nullptr)
	{}
};
template <class K, class V>
class RBTree
{  
	using Node = RBTreeeNode<K, V>;
public:
	bool Insert(const pair<K, V>& kv)
	{
		if (_root == nullptr)
		{
			_root = new Node(kv);
			_root->_col = BLACK;
			return true;
		}
		Node* cur = _root;
		Node* parent = nullptr;
		//找插入的节点
		while (cur)
		{
			if (cur->_kv.first > kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (cur->_kv.first < kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				return false;
			}
		}
		cur = new Node(kv);
		cur->_col = RED;
		//cur与parent的链接
		if ( parent->_kv.first > cur->_kv.first )
		{
			parent->_left = cur;
		}
		else
		{
			parent->_right = cur;
		}
		//链接父亲
		cur->_parent = parent;
		while (parent&& parent->_col == RED )
		{
			Node* grandfather = parent->_parent;
			if( grandfather->_left==parent )
			{
				// g
			   //p   u
			 //c
				Node* uncle = grandfather->_right;
				if (uncle && uncle->_col == RED)
				{
					//叔叔节点存在且为红色
					//变色
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;

					//继续向上更新处理
					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					//叔叔节点存在且为黑色，此时cur不是新增节点而是跟新的来的
					//叔叔为黑色或叔叔不存在且cur在左只需右单旋并变色,在else中叔叔存在与否的操作相同无需再判断 
					if (parent->_left == cur)
					{
						// g
				      //p   u
				    //c
						RotateR(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
					   // g                c
					  //p   u            p   g
					//    c                    u
						//cur在parent右，单旋无法解决问题，开始双旋
						RotateL(parent);
						RotateR(grandfather);
						//变色
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					break;
				}

			}
			else
			{
				// g
			   //u   p
			 //        c
				Node* uncle = grandfather->_left;
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;
					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
						if( parent->_right==cur )
						{
							RotateL(grandfather);
							parent->_col = BLACK;
							grandfather->_col = RED;
						}
						else
						{
							RotateR(parent);
							RotateL(grandfather);
							cur->_col = BLACK;
							grandfather->_col = RED;
						}
						break;
				}
			}
		}
		//变色结束后，根节点有可能被调整成RED此时将根节点置为BLACK省去了判断uncle不存在，很巧妙
		_root->_col = BLACK;
	}
	void RotateR(Node* parent)
	{
		//p
	  //sL
	//    sLR
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		Node* pParent = parent->_parent;
		parent->_left = subLR;
		if(subLR)
		{
			subLR->_parent = parent;
		}
		subL->_right = parent;
		parent->_parent = subL;
		if (_root == parent)
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else
		{
			if (pParent->_left == parent)
			{
				pParent->_left == subL;
			}
			else
			{
				pParent->_right == subL;
			}
			subL->_parent = pParent;
		}

	}
	void RotateL(Node* parent)
	{
		//p
      //   subr
		//subrl
		Node* subR = parent->_right;
		Node* subRL = subR->_left;
		Node* pParent = parent->_parent;

		parent->_right = subRL;
		if (subRL)
		{
			subRL->_parent = parent;
		}
		subR->_left = parent;
		parent->_parent = subR;
		if (parent == _root)
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (pParent->_left = parent)
			{
				pParent->_left = subR;
			}
			else
			{
				pParent->_right = subR;
			}
			subR->_parent = pParent;
		}
	}

	Node* Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_kv.first > key)
			{
				cur = cur->_left;
			}
			else if (cur->_kv.first < key)
			{
				cur = cur->_right;
			}
			else
			{
				return cur;
			}
		}
		return nullptr;
	}
	bool IsBalance()
	{
		if (_root == nullptr)
		{
			return true;
		}
		if (_root->_col == RED)
		{
			return false;
		}
		Node* cur = _root;
		int standardnum = 0;
		while (cur)
		{
			if (cur->_col == BLACK)
			{
				++standardnum;
			}
			cur = cur->_left;
		}
		return Check(_root, 0, standardnum);
	}
	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}
	int Size()
	{
		return _Size(_root);
	}
	int Height()
	{
		return _Height(_root);
	}
	private:
	int _Size(Node* root)
	{
		if (root == nullptr)
		{
			return 0;
		}
		return Size(root->_left) + Size(root->_right) + 1;
	}
	int _Height(Node* root)
	{
		if (root == nullptr)
		{
			return 0;
		}
		int LH = _Height(root->_left);
		int RH = _Height(root->_right);
		return LH > RH?LH + 1:RH+1;
	}
	

		void _InOrder(Node* root)
		{
			if (root == nullptr)
			{
				return;
			}
			_InOrder(root->_left);
			cout << root->_kv.first << ":" << root->_kv.second << endl;
			_InOrder(root->_right);
		}
		bool Check(Node* root, int blacknum, const int standardnum)
		{
			if (root == nullptr)
			{
				//一条路径走完若黑色节点不等于标准节点数，则出问题
				if (blacknum != standardnum)
				{
					//cout << "存在⿊⾊结点的数量不相等的路径" << endl;
					return false;
				}
				return true;
			}
			//孩子节点不好判断，找父亲节点逆着判断
			if (root->_col == RED && root->_parent->_col == RED)
			{
				//cout << root->_kv.first << "存在连续的红⾊结点" << endl;
				return false;
			}
			if (root->_col == BLACK)
			{
				++blacknum;
			}
			return  Check(root->_left, blacknum, standardnum) && Check(root->_right, blacknum, standardnum);
		}
private:
	Node* _root = nullptr;
};